package com.ktjiaoyu.yuanzihuan.controller;

import com.ktjiaoyu.yuanzihuan.bean.Movie;
import com.ktjiaoyu.yuanzihuan.config.CommonUtil;
import com.ktjiaoyu.yuanzihuan.config.WxConfig;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jboss.logging.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class IndexController {

    @RequestMapping(value = "/index")
    public String index() {
        System.out.println("index.........");
        return "index";
    }

    @RequestMapping(value = "/get")
    @ResponseBody
    public List<Movie> findAllRoles() {
        Movie movie1 = new Movie(1, "https://www.yuanxiaoxiang.com/demo/images/1.jpg", "肖申克的救赎", 1994, "弗兰克·德拉邦特", 8);
        Movie movie2 = new Movie(2, "https://www.yuanxiaoxiang.com/demo/images/2.jpg", "这个杀手不太冷", 1993, "吕克·贝松", 9);
        List<Movie> list = new ArrayList<Movie>();
        list.add(movie1);
        list.add(movie2);
        return list;
    }


    @ResponseBody
    @RequestMapping(value = "/getOpenId", method = RequestMethod.GET) // 获取用户信息
    public String getOpenId(@RequestParam("code") String code, @RequestParam("headurl") String headurl,
                            @RequestParam("nickname") String nickname, @RequestParam("sex") String sex,
                            @RequestParam("country") String country, @RequestParam("province") String province,
                            @RequestParam("city") String city) {
        System.out.println("getOpenId........................");
        String WX_URL = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
        System.out.println("code>>>>>>>>>>>" + code);
        System.out.println("headurl>>>>>>>>>>>" + headurl);
        System.out.println("nickname>>>>>>>>>>>" + nickname);
        System.out.println("sex>>>>>>>>>>>" + sex);
        System.out.println("country>>>>>>>>>>>" + country);
        System.out.println("province>>>>>>>>>>>" + province);
        System.out.println("city>>>>>>>>>>>" + city);
        try {
            if (StringUtils.isBlank(code)) {
                System.out.println("code为空");
            } else {
                String requestUrl = WX_URL.replace("APPID", WxConfig.APPID).replace("SECRET", WxConfig.APPSECRECT)
                        .replace("JSCODE", code).replace("authorization_code", WxConfig.GRANTTYPE);
                JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
                System.out.println("requestUrl........................" + requestUrl);
                System.out.println("jsonObject........................" + jsonObject);
                System.out.println("jsonObject........................" + jsonObject.toString());
                if (jsonObject != null) {
                    try {
                        // 业务操作
                        String openid = jsonObject.getString("openid");
                        String session_key = jsonObject.getString("session_key");
                        System.out.println("jsonObject.toString()>>>>>>>>>>" + jsonObject.toString());
                        System.out.println("openid>>>>>>>>>>" + openid);
                        System.out.println("session_key>>>>>>>>>>" + session_key);
                        //wechatService.selectUserByOpenId(openid, headurl, nickname, sex, country, province, city);
                        //return jsonObject.toString();
                        return openid;
                    } catch (Exception e) {
                        System.out.println("业务操作失败");
                        e.printStackTrace();
                    }
                } else {
                    System.out.println("code无效");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "错误";
    }
}
